package com.hongdee.scm.inventory.controller;

import com.alibaba.druid.util.StringUtils;
import com.hongdee.base.entity.ResultEntity;
import com.hongdee.common.util.DateUtils;
import com.hongdee.common.util.StringUtil;
import com.hongdee.scm.inventory.entity.TScmInventory;
import com.hongdee.scm.inventory.service.ITScmInventoryService;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.util.List;
import java.util.Map;

/**
 * 名  称:TScmInventoryController
 * 类  型:class
 * 描  述:
 * 路  径:com.hongdee.scm.inventory.controller
 * 作  者: 术洪超
 * 日  期:2017-06-01 14:45
 */
@RestController
@RequestMapping("/scm/inventory")
public class TScmInventoryController {

    @Autowired
    private ITScmInventoryService itScmInventoryService;

    /**
     * @param uid   登录用户 ID
     * @param token 登录用户 Token
     * @param id    主键 ID
     * @return ResultEntity
     * @throws Exception
     * @desc 查询详情
     * @author 术洪超
     */
    @RequestMapping("/info")
    @ResponseBody
    public ResultEntity info(@RequestParam(name = "uid", defaultValue = "") String uid,
                             @RequestParam(name = "token", defaultValue = "") String token,
                             @RequestParam(name = "id", defaultValue = "") String id) throws Exception {
        ResultEntity res = new ResultEntity();
        if (StringUtils.isEmpty(id)) {
            throw new Exception("请选择任务！");
        }
        Map map = itScmInventoryService.info(id);
        res.setData(map);
        res.setSuccess(true);
        res.setMsg("查询详情成功");
        return res;
    }


    /**
     * @param data 要保存的实体
     * @return Map
     * @throws Exception
     * @desc 保存添加或者修改的实体内容，通过主键判断
     * @author 术洪超
     */
    @RequestMapping("/save")
    @ResponseBody
    public ResultEntity save(@RequestParam(name = "uid", defaultValue = "") String uid,
                             @RequestParam(name = "token", defaultValue = "") String token,
                             @RequestBody Map data) throws Exception {
        TScmInventory tm = this.itScmInventoryService.saveOrUpdate(data, uid);
        ResultEntity res = new ResultEntity();
        res.setMsg("保存成功！");
        res.setSuccess(true);
        res.setData(tm);
        return res;
    }

    /**
     * @param data 要保存的实体
     * @return Map
     * @throws Exception
     * @desc 保存添加或者修改的实体内容，通过主键判断
     * @author 术洪超
     */
    @RequestMapping("/update")
    @ResponseBody
    public ResultEntity update(@RequestParam(name = "uid", defaultValue = "") String uid,
                               @RequestParam(name = "token", defaultValue = "") String token,
                               @RequestBody Map data) throws Exception {
        //ResultEntity res = this.itScmInventoryService.update(data, uid);
        ResultEntity res = new ResultEntity();
        itScmInventoryService.update(data, uid);
        res.setMsg("保存成功！");
        res.setSuccess(true);
        return res;
    }


    /**
     * @param uid   登录用户 ID
     * @param token 登录用户 Token
     * @param start 起始记录
     * @param limit 页面大小
     * @return Map
     * @throws Exception
     * @desc 通过 JDBCDao分页查询
     * @author 术洪超
     */
    @RequestMapping("/page")
    @ResponseBody
    public Map page(@RequestParam(name = "uid", defaultValue = "") String uid,
                    @RequestParam(name = "token", defaultValue = "") String token,
                    @RequestParam(name = "start", defaultValue = "0") String start,
                    @RequestParam(name = "limit", defaultValue = "50") String limit,
                    @RequestBody Map data) throws Exception {

        Map map = itScmInventoryService.page(start, limit, data, uid);
        map.put("success", true);
        map.put("msg", "分页查询成功！");
        return map;
    }

    /**
     * 车辆列表查询
     *
     * @param uid
     * @param token
     * @param data
     * @return
     * @throws Exception
     */
    @RequestMapping("/carPage")
    @ResponseBody
    public Map carPage(@RequestParam(name = "uid", defaultValue = "") String uid,
                       @RequestParam(name = "token", defaultValue = "") String token,
                       @RequestParam(name = "start", defaultValue = "0") String start,
                       @RequestParam(name = "limit", defaultValue = "50") String limit,
                       @RequestParam(name = "warehouseId", defaultValue = "") String warehouseId,
                       @RequestBody Map data) throws Exception {

        return this.itScmInventoryService.carPage(start, limit, data, uid, warehouseId);
    }

    /**
     * @param uid        登录用户 ID
     * @param token      登录用户 Token
     * @param locationId 仓库 ID
     * @return ResultEntity
     * @throws Exception
     * @desc 查询详情
     * @author 术洪超
     */
    @RequestMapping("/carCount")
    @ResponseBody
    public ResultEntity carCount(@RequestParam(name = "uid", defaultValue = "") String uid,
                                 @RequestParam(name = "token", defaultValue = "") String token,
                                 @RequestParam(name = "locationId", defaultValue = "") String locationId
    ) throws Exception {
        ResultEntity res = new ResultEntity();
        if (StringUtils.isEmpty(locationId)) {
            throw new Exception("请选择盘点单位！");
        }
        Map map = itScmInventoryService.carCount(locationId, uid);
        res.setData(map);
        res.setSuccess(true);
        res.setMsg("查询车辆数量成功");
        return res;
    }


    @RequestMapping("/excel/export")
    @ResponseBody
    public ResultEntity exportExcel(HttpServletRequest request, HttpServletResponse response,
                                    @RequestParam(name = "uid", defaultValue = "") String uid,
                                    @RequestParam(name = "token", defaultValue = "") String token,
                                    @RequestParam(name = "warehouseId", defaultValue = "") String warehouseId,
                                    @RequestBody Map data) throws Exception  {
      List<Map<String,Object>> lists=itScmInventoryService.findExcel(uid,  warehouseId,data);
        System.out.print("####"+lists.size());
        HSSFWorkbook wb=itScmInventoryService.export(lists, uid);
        String path = request.getSession().getServletContext().getRealPath(".");//服务器下项目的根路径
        String fileId = StringUtil.UUID();
        path =   path + File.separator + "upload";
        File filepack = new File(path);
        if(!filepack.exists()){
            filepack.mkdir();
        }
        String filePath =   path + File.separator + fileId + ".xlsx";  // File.separator="\"
        System.out.println("文件路径：" + filePath);
        File file = new File(filePath);
        file.createNewFile();
        FileOutputStream fileOut = new FileOutputStream(file);
        wb.write(fileOut);
        fileOut.close();
        ResultEntity res = new ResultEntity();
        res.setSuccess(true);
        res.setData(fileId);
        res.setMsg("文件生成成功！");
        return res;
    }

    /**
     * 导出盘点车辆
     * @param uid
     * @param inventoryId
     * @param request
     * @param response
     * @throws Exception
     */
    @RequestMapping("/exportCar")
    public void exportCar( @RequestParam(name = "uid", defaultValue = "") String uid,
                             @RequestParam(name = "token", defaultValue = "") String token,
                             @RequestParam(name = "inventoryId", defaultValue = "") String inventoryId,
                             HttpServletRequest request,
                             HttpServletResponse response)
     throws Exception {

        List lists = itScmInventoryService.findList(uid,inventoryId);

        HSSFWorkbook wb = itScmInventoryService.exportCar(lists, uid);
        response.setContentType("application/vnd.ms-excel");
        response.setCharacterEncoding("utf-8");
        String fileName = "盘点车辆";
        response.setHeader("Content-disposition", "attachment;filename=" + new String(fileName.getBytes("gbk"),"iso8859-1") +DateUtils.getTimeStr("yyyyMMddHHmmss").toString() + ".xls ");
        OutputStream ouputStream = response.getOutputStream();
        wb.write(ouputStream);
        ouputStream.flush();
        ouputStream.close();

    }
    /**
     * 根据导入的车辆修改盘点明细表
     * @param uid
     * @param token
     * @param inventoryId
     * @return
     * @throws Exception
     */
    @RequestMapping("/importCar")
    public ResultEntity importCar( @RequestParam(name = "uid", defaultValue = "") String uid,
                           @RequestParam(name = "token", defaultValue = "") String token,
                           @RequestParam(name = "inventoryId", defaultValue = "") String inventoryId)
            throws Exception {
        this.itScmInventoryService.importCar(uid,inventoryId);
        ResultEntity res = new ResultEntity();
        res.setSuccess(true);
        res.setMsg("修改成功！");
        return res;
    }

    /**
     * 手动输入盘点正常
     * @param uid
     * @param token
     * @param data
     * @return
     * @throws Exception
     */
    @RequestMapping("/updateRight")
    @ResponseBody
    public ResultEntity updateRight(@RequestParam(name = "uid", defaultValue = "") String uid,
                                    @RequestParam(name = "token", defaultValue = "") String token,
                                    @RequestBody Map data) throws Exception {
        ResultEntity res = new ResultEntity();
        this.itScmInventoryService.updateRight(data,uid);
        res.setSuccess(true);
        res.setMsg("盘点结果录入成功！");
        return res;
    }

    /**
     * 手动输入盘亏
     * @param uid
     * @param token
     * @param data
     * @return
     * @throws Exception
     */
    @RequestMapping("/updateError")
    @ResponseBody
    public ResultEntity updateError(@RequestParam(name = "uid", defaultValue = "") String uid,
                                    @RequestParam(name = "token", defaultValue = "") String token,
                                    @RequestBody Map data) throws Exception {
        ResultEntity res = new ResultEntity();
        this.itScmInventoryService.updateError(data,uid);
        res.setSuccess(true);
        res.setMsg("盘点结果录入成功！");
        return res;
    }

    /**
     * 盘点确认
     * @param uid
     * @param token
     * @param inventoryId
     * @return
     * @throws Exception
     */
    @RequestMapping("/confirmInventory")
    public ResultEntity confirmInventory( @RequestParam(name = "uid", defaultValue = "") String uid,
                                   @RequestParam(name = "token", defaultValue = "") String token,
                                   @RequestParam(name = "inventoryId", defaultValue = "") String inventoryId)
    throws Exception {
        this.itScmInventoryService.confirmInventory(inventoryId,uid);
        ResultEntity res = new ResultEntity();
        res.setSuccess(true);
        res.setMsg("修改成功！");
        return res;
    }
}
